<!--
config.optionalTime : Boolean 是否可选时间
-->

<template>
  <el-date-picker
    v-model="inputVal"
    :disabled="isReadonly"
    @change="change"
    :type="type"
    :value-format="format"
    :placeholder="isReadonly ? '' : `请选择${config.name}`"
  >
  </el-date-picker>
</template>
<script>
export default {
  props: {
    isReadonly: {
      type: Boolean,
      default: false,
    },
    formData: Object,
    config: {
      type: Object,
      default: () => Object.create(null),
    },
  },
  data() {
    return {
      inputVal: null,
      type: "date",
    };
  },
  created() {
    if (this.config.optionalTime) {
      this.type = "datetime";
    }
  },
  computed: {
    format() {
      if (!this.config.optionalTime) {
        return "yyyy-MM-dd";
      } else if (this.config.optionalTime) {
        return "yyyy-MM-dd HH:mm:ss";
      }
    },
    curVal() {
      return this.formData[this.config.prop] || "";
    },
  },
  watch: {
    curVal: {
      immediate: true,
      deep: true,
      handler(val) {
        this.inputVal = val;
      },
    },
  },
  methods: {
    change(val) {
      this.inputVal = val;
      this.formData[this.config.prop] = val;
      this.$emit("update:formData", this.formData);
      this.$emit("change");
    },
  },
  mounted() {},
};
</script>
<style lang="scss" scoped></style>
